package com.ukao.steward.ui.valetRunners.storage;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.bm.library.PhotoView;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.ukao.steward.R;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.util.ImageUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PhotoDetailFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PhotoDetailFragment extends BaseFragment  implements RequestListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.image)
    PhotoView image;
    @BindView(R.id.pb_load_local)
    ProgressBar progressBar;
    Unbinder unbinder;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    public boolean mHasLoadedOnce;
    private String mImageUrl;

    public static PhotoDetailFragment newInstance(String param1, String param2) {
        PhotoDetailFragment fragment = new PhotoDetailFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mImageUrl = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_photo_detail, container, false);
        unbinder = ButterKnife.bind(this, view);
        image.enable();
        return view;
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (mHasLoadedOnce) return;
        ImageUtils.loadImage(_mActivity, mImageUrl, image, this);

    }

    @Override
    public boolean onException(Exception e, Object o, Target target, boolean b) {
        if (progressBar != null)
            progressBar.setVisibility(View.GONE);


        return false;
    }

    @Override/*加载成功*/
    public boolean onResourceReady(Object o, Object o2, Target target, boolean b, boolean b1) {
        mHasLoadedOnce = true;
        if (progressBar != null)
            progressBar.setVisibility(View.GONE);
        return false;
    }


    @Override
    protected void initView() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
